﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Silverlight.Arcgis.MapTip.GetDate
{
    public class JsonGetData:IGetData
    {

        /// <summary>
        /// 当前的JSON数据对像
        /// </summary>
        private System.Json.JsonValue jsObj = null;

        #region 处理得到JSon数据
        /// <summary>
        /// 转换成为 Js 对像
        /// </summary>
        /// <param name="str"></param>
        void ConverToJsobject(string str)
        {

            
        

           
            #region 线程处理的代码体

            System.Threading.Thread Thr = new System.Threading.Thread(new System.Threading.ThreadStart(
                () =>
                {
                    jsObj = System.Json.JsonArray.Parse(str);
                    Data = jsObj;
                    //得到总数
                    DataCount = jsObj.Count;

                    int k=0;
                    ///模拟进度
                    foreach (var p in jsObj)
                    {
                        DataCurrProgerss = k++;
                        System.Threading.Thread.Sleep(1);
                    }



                    ///得到数据完成
                    if (GetDataComplete != null)
                    {
                        GetDataComplete(this, new GetDateEventArgs(jsObj, 100));
                    }

                }
                ));
            Thr.Start();
            
            #endregion

        }

        /// <summary>
        /// 转换成JS对像的委托
        /// </summary>
        /// <param name="str"></param>
        delegate void DelConverToJsobject(string Vstr);




        #endregion


        #region IGetData 成员

        /// <summary>
        /// 得到数据的主方法
        /// </summary>
        /// <param name="uri"></param>
        public void GetDate(Uri uri)
        {
            //声明一个从URL得到数据的对像
            Bocom.Silverlight.LoadUrlString LoadUrlstr = new Bocom.Silverlight.LoadUrlString();

            string stru = "";

            //邦定数据源
            LoadUrlstr.StartDownLoad(uri, stru);

            LoadUrlstr.LoadUrlStringCompleted += (object sen, Bocom.Silverlight.LoadUrlString.LoadUrlStringEventArgs ev) =>
            {
                ///处理数据
                ConverToJsobject(ev.Str);
            };

            ///加载的进度
            LoadUrlstr.LoadUrlStringProgress += (object sen, Bocom.Silverlight.LoadUrlString.LoadUrlStringEventArgsForProgress ev) =>
            {
                Progerss = ev.Prog;
            };
        }
        
        /// <summary>
        /// 得到数据完成事件
        /// </summary>
        public event DelegateGetDate GetDataComplete;


        /// <summary>
        /// 当前加载数据的进度
        /// </summary>
        private double progerss = 0;

        /// <summary>
        /// 当前加载数据的进度
        /// </summary>
        public double Progerss
        {
            get
            {
                return progerss;
            }
            set
            {
                progerss = value;
                ///注册属性值改变事件
                RegPropertyChanged("Progerss");
            }
        }

        /// <summary>
        /// 当前的数据
        /// </summary>
        private object data = null;

        /// <summary>
        /// 当前的数据
        /// </summary>
        public object Data
        {
            get
            {
                return data;
            }
            set
            {
                data = value;
                //注册属性值改变事件
                RegPropertyChanged("Data");

            }
        }

        #endregion

        #region INotifyPropertyChanged 成员

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;


        /// <summary>
        /// 注册属性值改变事件
        /// </summary>
        /// <param name="PropertyName">属性名</param>
        private void RegPropertyChanged(string PropertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(PropertyName));
            }
        }

        #endregion


        /// <summary>
        /// 当前数据总数
        /// </summary>
        private int dataCount = 0;


        /// <summary>
        /// 当前数据的总数
        /// </summary>
        public int DataCount
        {
            get
            {
                return dataCount;
            }
            set
            {
                dataCount = value;
                //注册属性值发生改变
                RegPropertyChanged("DataCount");
            }
        }

        /// <summary>
        /// 当前处理的条数
        /// 转换是JSON对像的进度
        /// </summary>
        private double dataCurrProgerss = 0;

        /// <summary>
        /// 当前处理的条数
        /// 转换是JSON对像的进度
        /// </summary>
        public double DataCurrProgerss
        {
            get
            {
                return dataCurrProgerss;
            }
            set
            {
                dataCurrProgerss = value;
                //注册属性值发生改变的事件
                RegPropertyChanged("DataCurrProgerss");
            }
        }




    }
}
